<!DOCTYPE html>

<html
  xmlns="http://www.w3.org/1999/xhtml"
  >
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="generator" content="pandoc" />
    <link rel="icon" href="img/logo/data-to-viz.ico" />

    <style>
      .myheader {
        background-image: url("https://github.com/holtzy/data_to_viz/raw/master/img/overview_RGG_faded.png");
        min-height: 350px;
        background-size: 100% 100%;

        /* Create the parallax scrolling effect */
        background-attachment: fixed;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;

        height: 100%;
        width: 100% !important;
      }
      .mytitle {
        text-transform: uppercase;
        font-family: "Montserrat";
        font-size: 200%;
        font-weight: 500;
        letter-spacing: 4px;
      }
    </style>

    <div class="myheader" style="text-align: center">
      <br />
      <a href="http://www.data-to-viz.com"
        ><img
          align="left"
          src="https://github.com/holtzy/data_to_viz/raw/master/img/logo/typo.png"
          style="width: 150px; vertical-align: top; margin-left: 5%"
      /></a>
      <br /><br />
      <br /><br />
      <p class="mytitle">Perception of probability</p>
      <center>
        <div class="mysubtitle">
          <i
            >A few data analytics ideas from
            <a href="https://www.data-to-viz.com">Data-to-Viz.com</a></i
          >
        </div>
      </center>
      <br /><br />

            <a href="https://www.data-to-viz.com/#portfolioanchor">
        <img class="mylogoheader"
        src=https://github.com/holtzy/data_to_viz/raw/master/img/section/DensitySmall.png></a
      >
             <a href="https://www.data-to-viz.com/#portfolioanchor">
        <img class="mylogoheader"
        src=https://github.com/holtzy/data_to_viz/raw/master/img/section/HistogramSmall.png></a
      >
             <a href="https://www.data-to-viz.com/#portfolioanchor">
        <img class="mylogoheader"
        src=https://github.com/holtzy/data_to_viz/raw/master/img/section/Box1Small.png></a
      >
             <a href="https://www.data-to-viz.com/#portfolioanchor">
        <img class="mylogoheader"
        src=https://github.com/holtzy/data_to_viz/raw/master/img/section/ViolinSmall.png></a
      >
             <a href="https://www.data-to-viz.com/#portfolioanchor">
        <img class="mylogoheader"
        src=https://github.com/holtzy/data_to_viz/raw/master/img/section/JoyplotSmall.png></a
      >
               </div>

       
    <title>OneNumOneCatSeveralObs.knit</title>

     <script src="libs/header-attrs-2.27/header-attrs.js"></script>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="libs/bootstrap-3.3.5/css/lumen.min.css" rel="stylesheet" />
<script src="libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
       h1.title {font-size: 38px;}
       h2 {font-size: 30px;}
       h3 {font-size: 24px;}
       h4 {font-size: 18px;}
       h5 {font-size: 16px;}
       h6 {font-size: 12px;}
       code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
       pre:not([class]) { background-color: white }</style>
<script src="libs/navigation-1.1/tabsets.js"></script>
<script src="libs/navigation-1.1/codefolding.js"></script>
<link href="libs/pagedtable-1.1/css/pagedtable.css" rel="stylesheet" />
<script src="libs/pagedtable-1.1/js/pagedtable.js"></script>
<script src="libs/kePrint-0.0.1/kePrint.js"></script>
<link href="libs/lightable-0.0.1/lightable.css" rel="stylesheet" />       <style type="text/css">
      code {
        white-space: pre;
      }
    </style>
    <style type="text/css">
      pre > code.sourceCode { white-space: pre; position: relative; }
      pre > code.sourceCode > span { line-height: 1.25; }
      pre > code.sourceCode > span:empty { height: 1.2em; }
      .sourceCode { overflow: visible; }
      code.sourceCode > span { color: inherit; text-decoration: inherit; }
      div.sourceCode { margin: 1em 0; }
      pre.sourceCode { margin: 0; }
      @media screen {
      div.sourceCode { overflow: auto; }
      }
      @media print {
      pre > code.sourceCode { white-space: pre-wrap; }
      pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
      }
      pre.numberSource code
        { counter-reset: source-line 0; }
      pre.numberSource code > span
        { position: relative; left: -4em; counter-increment: source-line; }
      pre.numberSource code > span > a:first-child::before
        { content: counter(source-line);
          position: relative; left: -1em; text-align: right; vertical-align: baseline;
          border: none; display: inline-block;
          -webkit-touch-callout: none; -webkit-user-select: none;
          -khtml-user-select: none; -moz-user-select: none;
          -ms-user-select: none; user-select: none;
          padding: 0 4px; width: 4em;
          color: #aaaaaa;
        }
      pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
      div.sourceCode
        {   }
      @media screen {
      pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
      }
      code span.al { color: #ff0000; font-weight: bold; } /* Alert */
      code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
      code span.at { color: #7d9029; } /* Attribute */
      code span.bn { color: #40a070; } /* BaseN */
      code span.bu { color: #008000; } /* BuiltIn */
      code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
      code span.ch { color: #4070a0; } /* Char */
      code span.cn { color: #880000; } /* Constant */
      code span.co { color: #60a0b0; font-style: italic; } /* Comment */
      code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
      code span.do { color: #ba2121; font-style: italic; } /* Documentation */
      code span.dt { color: #902000; } /* DataType */
      code span.dv { color: #40a070; } /* DecVal */
      code span.er { color: #ff0000; font-weight: bold; } /* Error */
      code span.ex { } /* Extension */
      code span.fl { color: #40a070; } /* Float */
      code span.fu { color: #06287e; } /* Function */
      code span.im { color: #008000; font-weight: bold; } /* Import */
      code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
      code span.kw { color: #007020; font-weight: bold; } /* Keyword */
      code span.op { color: #666666; } /* Operator */
      code span.ot { color: #007020; } /* Other */
      code span.pp { color: #bc7a00; } /* Preprocessor */
      code span.sc { color: #4070a0; } /* SpecialChar */
      code span.ss { color: #bb6688; } /* SpecialString */
      code span.st { color: #4070a0; } /* String */
      code span.va { color: #19177c; } /* Variable */
      code span.vs { color: #4070a0; } /* VerbatimString */
      code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
            div.sourceCode {
        overflow-x: visible;
      }
          </style>
        <style type="text/css">
      pre:not([class]) {
        background-color: white;
      }
    </style>
           <style type="text/css">
      h1 {
        font-size: 34px;
      }
      h1.title {
        font-size: 38px;
      }
      h2 {
        font-size: 30px;
      }
      h3 {
        font-size: 24px;
      }
      h4 {
        font-size: 18px;
      }
      h5 {
        font-size: 16px;
      }
      h6 {
        font-size: 12px;
      }
      .table th:not([align]) {
        text-align: left;
      }
          </style>
         <link
      rel="stylesheet"
      href="style.css"
      type="text/css"
      
    />
      </head>

  <body>
        <style type="text/css">
      .main-container {
        max-width: 940px;
        margin-left: auto;
        margin-right: auto;
      }
      code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
      }
      img {
        max-width: 100%;
        height: auto;
      }
      .tabbed-pane {
        padding-top: 12px;
      }
      button.code-folding-btn:focus {
        outline: none;
      }
    </style>

     
    <div class="container-fluid main-container">
      <!-- tabsets -->
      <script>
        $(document).ready(function () {
          window.buildTabsets("TOC");
        });
      </script>

      <!-- code folding -->
            <style type="text/css">
        .code-folding-btn {
          margin-bottom: 4px;
        }
      </style>
      <script>
        $(document).ready(function () {
                          window.initializeCodeFolding("hide" === "show");
                });
      </script>
         
                    <div class="fluid-row" id="header">
                        <div class="btn-group pull-right">
              <button
                type="button"
                class="btn btn-default btn-xs dropdown-toggle"
                data-toggle="dropdown"
                aria-haspopup="true"
                aria-expanded="false"
              >
                <span>Code</span> <span class="caret"></span>
              </button>
              <ul class="dropdown-menu" style="min-width: 50px">
                                <li><a id="rmd-show-all-code" href="#">Show All Code</a></li>
                <li><a id="rmd-hide-all-code" href="#">Hide All Code</a></li>
                                </ul>
            </div>

                         </div>
              <br><br><br>
<div class="mycontent">





<div class="row">
<div class="col-md-8">
<p><br><br> This document gives a few suggestions to analyse a dataset
composed by a numeric and a categoric variable. <br><br> On the <a
href="https://www.reddit.com/r/SampleSize/">/r/samplesize</a> thread of
reddit, questions like <em>What probability would you assign to the
phrase “Highly likely”</em> were asked. Results allow to understand how
people perceive probability vocabulary. <br><br>
<strong>Disclaimer</strong>: This idea originally comes from a
publication of the <a
href="https://www.cia.gov/library/center-for-the-study-of-intelligence/csi-publications/books-and-monographs/psychology-of-intelligence-analysis/art15.html">CIA</a>
which resulted in this <a
href="https://www.cia.gov/library/center-for-the-study-of-intelligence/csi-publications/books-and-monographs/psychology-of-intelligence-analysis/fig18.gif/image.gif">figure</a>.
Then, <a href="https://github.com/zonination">Zoni Nation</a> cleaned
the reddit dataset and built <a
href="https://github.com/zonination/perceptions">graphics with R</a>. I
heavily rely on his work in the folowing.</p>
</div>
<div class="col-md-4">
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="co"># Libraries</span></span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="fu">library</span>(hrbrthemes)</span>
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="fu">library</span>(kableExtra)</span>
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="fu">options</span>(<span class="at">knitr.table.format =</span> <span class="st">&quot;html&quot;</span>)</span>
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="fu">library</span>(viridis)</span>
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="co"># Load dataset from github</span></span>
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">read.table</span>(<span class="st">&quot;https://raw.githubusercontent.com/zonination/perceptions/master/probly.csv&quot;</span>, <span class="at">header=</span><span class="cn">TRUE</span>, <span class="at">sep=</span><span class="st">&quot;,&quot;</span>)</span>
<span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a>data <span class="ot">&lt;-</span> data <span class="sc">%&gt;%</span></span>
<span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a>  <span class="fu">gather</span>(<span class="at">key=</span><span class="st">&quot;text&quot;</span>, <span class="at">value=</span><span class="st">&quot;value&quot;</span>) <span class="sc">%&gt;%</span></span>
<span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">gsub</span>(<span class="st">&quot;</span><span class="sc">\\</span><span class="st">.&quot;</span>, <span class="st">&quot; &quot;</span>,text)) <span class="sc">%&gt;%</span></span>
<span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">value =</span> <span class="fu">round</span>(<span class="fu">as.numeric</span>(value),<span class="dv">0</span>))</span>
<span id="cb1-14"><a href="#cb1-14" tabindex="-1"></a></span>
<span id="cb1-15"><a href="#cb1-15" tabindex="-1"></a></span>
<span id="cb1-16"><a href="#cb1-16" tabindex="-1"></a><span class="co"># show data</span></span>
<span id="cb1-17"><a href="#cb1-17" tabindex="-1"></a>data <span class="sc">%&gt;%</span> <span class="fu">sample_n</span>(<span class="dv">8</span>) <span class="sc">%&gt;%</span> <span class="fu">kable</span>(<span class="at">row.names =</span> <span class="cn">FALSE</span>) <span class="sc">%&gt;%</span></span>
<span id="cb1-18"><a href="#cb1-18" tabindex="-1"></a>  <span class="fu">kable_styling</span>(<span class="at">bootstrap_options =</span> <span class="st">&quot;striped&quot;</span>, <span class="at">full_width =</span> F)</span></code></pre></div>
<table class="table table-striped" style="width: auto !important; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;">
text
</th>
<th style="text-align:right;">
value
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
We Doubt
</td>
<td style="text-align:right;">
33
</td>
</tr>
<tr>
<td style="text-align:left;">
About Even
</td>
<td style="text-align:right;">
50
</td>
</tr>
<tr>
<td style="text-align:left;">
Almost No Chance
</td>
<td style="text-align:right;">
1
</td>
</tr>
<tr>
<td style="text-align:left;">
Highly Unlikely
</td>
<td style="text-align:right;">
5
</td>
</tr>
<tr>
<td style="text-align:left;">
Almost No Chance
</td>
<td style="text-align:right;">
2
</td>
</tr>
<tr>
<td style="text-align:left;">
Improbable
</td>
<td style="text-align:right;">
3
</td>
</tr>
<tr>
<td style="text-align:left;">
Chances Are Slight
</td>
<td style="text-align:right;">
40
</td>
</tr>
<tr>
<td style="text-align:left;">
We Doubt
</td>
<td style="text-align:right;">
10
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="boxplot" class="section level1">
<h1>Boxplot</h1>
<hr />
<p>The most common way to represent that kind of dataset is probably the
<a href="">boxplot</a>. It summarizes the main features of each group
and thus allow an efficient of distributions. Mind a few pitfalls tough.
It often makes sense to <a
href="http://www.data-to-viz.com/caveat/order_data.html">order
groups</a> to make the chart easier to read. If the <a
href="http://www.data-to-viz.com/caveat/hard_label.html">group labels
are long</a>, consider a horizontal version that makes the labels
readable. Last but not least, note that the boxplot <a
href="http://www.data-to-viz.com/caveat/boxplot.html">hides
information</a> like the underlying distribution of the sample size. <a
href="http://www.data-to-viz.com/caveat/boxplot.html">Several
workarounds</a> exist, like showing the individual data points with
unobstrusive dots.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>data <span class="sc">%&gt;%</span></span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">fct_reorder</span>(text, value)) <span class="sc">%&gt;%</span></span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a>  <span class="fu">ggplot</span>( <span class="fu">aes</span>(<span class="at">x=</span>text, <span class="at">y=</span>value, <span class="at">fill=</span>text)) <span class="sc">+</span></span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a>    <span class="fu">geom_boxplot</span>() <span class="sc">+</span></span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a>    <span class="fu">geom_jitter</span>(<span class="at">color=</span><span class="st">&quot;grey&quot;</span>, <span class="at">alpha=</span><span class="fl">0.3</span>, <span class="at">size=</span><span class="fl">0.9</span>) <span class="sc">+</span></span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a>    <span class="fu">scale_fill_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a>    <span class="fu">theme_ipsum</span>() <span class="sc">+</span></span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a>    <span class="fu">theme</span>(</span>
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a>      <span class="at">legend.position=</span><span class="st">&quot;none&quot;</span></span>
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a>    ) <span class="sc">+</span></span>
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a>    <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a>    <span class="fu">xlab</span>(<span class="st">&quot;&quot;</span>) <span class="sc">+</span></span>
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a>    <span class="fu">ylab</span>(<span class="st">&quot;Assigned Probability (%)&quot;</span>)</span></code></pre></div>
<p><img src="OneNumOneCatSeveralObs_files/figure-html/unnamed-chunk-2-1.png" width="768" style="display: block; margin: auto;" /></p>
</div>
<div id="violin" class="section level1">
<h1>Violin</h1>
<hr />
<p>The <a href="">violin plot</a> is often a good alternative to boxplot
as long as your sample size is big enough. It is very close to the
boxplot, thus the advices above still apply, except that it describes
group distributions more accurately by definition. If you have many
groups (like here), it is probably not the best option since each violin
tends to be very slim, what makes hard to visualize the distribution. In
this case a good alternative is the <a href="">ridgeline plot</a> thatis
described a bit further in this post.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>data <span class="sc">%&gt;%</span></span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">fct_reorder</span>(text, value)) <span class="sc">%&gt;%</span></span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a>  <span class="fu">ggplot</span>( <span class="fu">aes</span>(<span class="at">x=</span>text, <span class="at">y=</span>value, <span class="at">fill=</span>text, <span class="at">color=</span>text)) <span class="sc">+</span></span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a>    <span class="fu">geom_violin</span>(<span class="at">width=</span><span class="fl">2.1</span>, <span class="at">size=</span><span class="fl">0.2</span>) <span class="sc">+</span></span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a>    <span class="fu">scale_fill_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a>    <span class="fu">scale_color_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a>    <span class="fu">theme_ipsum</span>() <span class="sc">+</span></span>
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a>    <span class="fu">theme</span>(</span>
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a>      <span class="at">legend.position=</span><span class="st">&quot;none&quot;</span></span>
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a>    ) <span class="sc">+</span></span>
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a>    <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a>    <span class="fu">xlab</span>(<span class="st">&quot;&quot;</span>) <span class="sc">+</span></span>
<span id="cb3-13"><a href="#cb3-13" tabindex="-1"></a>    <span class="fu">ylab</span>(<span class="st">&quot;Assigned Probability (%)&quot;</span>)</span></code></pre></div>
<p><img src="OneNumOneCatSeveralObs_files/figure-html/unnamed-chunk-3-1.png" width="768" style="display: block; margin: auto;" /></p>
</div>
<div id="density" class="section level1">
<h1>Density</h1>
<hr />
<p>If you have just a few group, you can compare them on the same <a
href="">density plot</a>. Here only four groups were selected to
illustrate this idea. With more groups, the graphic would get cluttered
and hard to read. The number of group you can show on the same density
plot depends of your data: if the group overlap, few, if their
distribution is really different, a bit more.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="co"># A dataframe for annotations</span></span>
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a>annot <span class="ot">&lt;-</span> <span class="fu">data.frame</span>(</span>
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a>  <span class="at">text =</span> <span class="fu">c</span>(<span class="st">&quot;Almost No Chance&quot;</span>, <span class="st">&quot;About Even&quot;</span>, <span class="st">&quot;Probable&quot;</span>, <span class="st">&quot;Almost Certainly&quot;</span>),</span>
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a>  <span class="at">x =</span> <span class="fu">c</span>(<span class="dv">5</span>, <span class="dv">53</span>, <span class="dv">65</span>, <span class="dv">79</span>),</span>
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a>  <span class="at">y =</span> <span class="fu">c</span>(<span class="fl">0.15</span>, <span class="fl">0.4</span>, <span class="fl">0.06</span>, <span class="fl">0.1</span>)</span>
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a>)</span>
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a></span>
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co"># Plot</span></span>
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a>data <span class="sc">%&gt;%</span></span>
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a>  <span class="fu">filter</span>(text <span class="sc">%in%</span> <span class="fu">c</span>(<span class="st">&quot;Almost No Chance&quot;</span>, <span class="st">&quot;About Even&quot;</span>, <span class="st">&quot;Probable&quot;</span>, <span class="st">&quot;Almost Certainly&quot;</span>)) <span class="sc">%&gt;%</span></span>
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">fct_reorder</span>(text, value)) <span class="sc">%&gt;%</span></span>
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a>  <span class="fu">ggplot</span>( <span class="fu">aes</span>(<span class="at">x=</span>value, <span class="at">color=</span>text, <span class="at">fill=</span>text)) <span class="sc">+</span></span>
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a>    <span class="fu">geom_density</span>(<span class="at">alpha=</span><span class="fl">0.6</span>) <span class="sc">+</span></span>
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a>    <span class="fu">scale_fill_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a>    <span class="fu">scale_color_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb4-16"><a href="#cb4-16" tabindex="-1"></a>    <span class="fu">geom_text</span>( <span class="at">data=</span>annot, <span class="fu">aes</span>(<span class="at">x=</span>x, <span class="at">y=</span>y, <span class="at">label=</span>text, <span class="at">color=</span>text), <span class="at">hjust=</span><span class="dv">0</span>, <span class="at">size=</span><span class="fl">4.5</span>) <span class="sc">+</span></span>
<span id="cb4-17"><a href="#cb4-17" tabindex="-1"></a>    <span class="fu">theme_ipsum</span>() <span class="sc">+</span></span>
<span id="cb4-18"><a href="#cb4-18" tabindex="-1"></a>    <span class="fu">theme</span>(</span>
<span id="cb4-19"><a href="#cb4-19" tabindex="-1"></a>      <span class="at">legend.position=</span><span class="st">&quot;none&quot;</span>,</span>
<span id="cb4-20"><a href="#cb4-20" tabindex="-1"></a>      <span class="at">panel.spacing =</span> <span class="fu">unit</span>(<span class="fl">0.1</span>, <span class="st">&quot;lines&quot;</span>),</span>
<span id="cb4-21"><a href="#cb4-21" tabindex="-1"></a>      <span class="at">strip.text.x =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">8</span>)</span>
<span id="cb4-22"><a href="#cb4-22" tabindex="-1"></a>    ) <span class="sc">+</span></span>
<span id="cb4-23"><a href="#cb4-23" tabindex="-1"></a>    <span class="fu">xlab</span>(<span class="st">&quot;&quot;</span>) <span class="sc">+</span></span>
<span id="cb4-24"><a href="#cb4-24" tabindex="-1"></a>    <span class="fu">ylab</span>(<span class="st">&quot;Assigned Probability (%)&quot;</span>)</span></code></pre></div>
<p><img src="OneNumOneCatSeveralObs_files/figure-html/unnamed-chunk-4-1.png" width="768" style="display: block; margin: auto;" /></p>
<p>However if you have more than ~4 groups this technique does not work:
the graphic would become too cluttered. Thus it is a better practice to
use small multiple:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>data <span class="sc">%&gt;%</span></span>
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">fct_reorder</span>(text, value)) <span class="sc">%&gt;%</span></span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a>  <span class="fu">ggplot</span>( <span class="fu">aes</span>(<span class="at">x=</span>value, <span class="at">color=</span>text, <span class="at">fill=</span>text)) <span class="sc">+</span></span>
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a>    <span class="fu">geom_density</span>(<span class="at">alpha=</span><span class="fl">0.6</span>) <span class="sc">+</span></span>
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a>    <span class="fu">scale_fill_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a>    <span class="fu">scale_color_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a>    <span class="fu">theme_ipsum</span>() <span class="sc">+</span></span>
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a>    <span class="fu">theme</span>(</span>
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a>      <span class="at">legend.position=</span><span class="st">&quot;none&quot;</span>,</span>
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a>      <span class="at">panel.spacing =</span> <span class="fu">unit</span>(<span class="fl">0.1</span>, <span class="st">&quot;lines&quot;</span>),</span>
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a>      <span class="at">strip.text.x =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">8</span>)</span>
<span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a>    ) <span class="sc">+</span></span>
<span id="cb5-13"><a href="#cb5-13" tabindex="-1"></a>    <span class="fu">xlab</span>(<span class="st">&quot;&quot;</span>) <span class="sc">+</span></span>
<span id="cb5-14"><a href="#cb5-14" tabindex="-1"></a>    <span class="fu">ylab</span>(<span class="st">&quot;Assigned Probability (%)&quot;</span>) <span class="sc">+</span></span>
<span id="cb5-15"><a href="#cb5-15" tabindex="-1"></a>    <span class="fu">facet_wrap</span>(<span class="sc">~</span>text, <span class="at">scale=</span><span class="st">&quot;free_y&quot;</span>)</span></code></pre></div>
<p><img src="OneNumOneCatSeveralObs_files/figure-html/unnamed-chunk-5-1.png" width="768" style="display: block; margin: auto;" /></p>
<p>This is a good way to study the distribution of each group
separately. However, it makes it hard to compare groups together since
they do not all share the same Y axis. All depends on what is the
question you are trying to answer.</p>
</div>
<div id="histogram" class="section level1">
<h1>Histogram</h1>
<hr />
<p>The <a href="">histrogram</a> is very close from density plots. Thus,
all the remarks above probably apply here as well.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>data <span class="sc">%&gt;%</span></span>
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">fct_reorder</span>(text, value)) <span class="sc">%&gt;%</span></span>
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a>  <span class="fu">ggplot</span>( <span class="fu">aes</span>(<span class="at">x=</span>value, <span class="at">color=</span>text, <span class="at">fill=</span>text)) <span class="sc">+</span></span>
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a>    <span class="fu">geom_histogram</span>(<span class="at">alpha=</span><span class="fl">0.6</span>, <span class="at">binwidth =</span> <span class="dv">5</span>) <span class="sc">+</span></span>
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a>    <span class="fu">scale_fill_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a>    <span class="fu">scale_color_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a>    <span class="fu">theme_ipsum</span>() <span class="sc">+</span></span>
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a>    <span class="fu">theme</span>(</span>
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a>      <span class="at">legend.position=</span><span class="st">&quot;none&quot;</span>,</span>
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a>      <span class="at">panel.spacing =</span> <span class="fu">unit</span>(<span class="fl">0.1</span>, <span class="st">&quot;lines&quot;</span>),</span>
<span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a>      <span class="at">strip.text.x =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">8</span>)</span>
<span id="cb6-12"><a href="#cb6-12" tabindex="-1"></a>    ) <span class="sc">+</span></span>
<span id="cb6-13"><a href="#cb6-13" tabindex="-1"></a>    <span class="fu">xlab</span>(<span class="st">&quot;&quot;</span>) <span class="sc">+</span></span>
<span id="cb6-14"><a href="#cb6-14" tabindex="-1"></a>    <span class="fu">ylab</span>(<span class="st">&quot;Assigned Probability (%)&quot;</span>) <span class="sc">+</span></span>
<span id="cb6-15"><a href="#cb6-15" tabindex="-1"></a>    <span class="fu">facet_wrap</span>(<span class="sc">~</span>text)</span></code></pre></div>
<p><img src="OneNumOneCatSeveralObs_files/figure-html/unnamed-chunk-6-1.png" width="768" style="display: block; margin: auto;" />
Note that here the Y scale is the same for each group, unlike the
precedent example on density plot.</p>
</div>
<div id="ridgeline-plot" class="section level1">
<h1>Ridgeline plot</h1>
<hr />
<p>In this example, the best option is probably the <a href="">ridgeline
plot</a>. It shares all the benefits of a violinplot and avoid to loose
space since there is an overlap between groups. Both individual
distribution and comparison between groups are described
efficiently.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="fu">library</span>(ggridges)</span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a>data <span class="sc">%&gt;%</span></span>
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">text =</span> <span class="fu">fct_reorder</span>(text, value)) <span class="sc">%&gt;%</span></span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a>  <span class="fu">ggplot</span>( <span class="fu">aes</span>(<span class="at">y=</span>text, <span class="at">x=</span>value,  <span class="at">fill=</span>text)) <span class="sc">+</span></span>
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a>    <span class="fu">geom_density_ridges</span>(<span class="at">alpha=</span><span class="fl">0.6</span>, <span class="at">bandwidth=</span><span class="dv">4</span>) <span class="sc">+</span></span>
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a>    <span class="fu">scale_fill_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a>    <span class="fu">scale_color_viridis</span>(<span class="at">discrete=</span><span class="cn">TRUE</span>) <span class="sc">+</span></span>
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a>    <span class="fu">theme_ipsum</span>() <span class="sc">+</span></span>
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a>    <span class="fu">theme</span>(</span>
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a>      <span class="at">legend.position=</span><span class="st">&quot;none&quot;</span>,</span>
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a>      <span class="at">panel.spacing =</span> <span class="fu">unit</span>(<span class="fl">0.1</span>, <span class="st">&quot;lines&quot;</span>),</span>
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a>      <span class="at">strip.text.x =</span> <span class="fu">element_text</span>(<span class="at">size =</span> <span class="dv">8</span>)</span>
<span id="cb7-13"><a href="#cb7-13" tabindex="-1"></a>    ) <span class="sc">+</span></span>
<span id="cb7-14"><a href="#cb7-14" tabindex="-1"></a>    <span class="fu">xlab</span>(<span class="st">&quot;&quot;</span>) <span class="sc">+</span></span>
<span id="cb7-15"><a href="#cb7-15" tabindex="-1"></a>    <span class="fu">ylab</span>(<span class="st">&quot;Assigned Probability (%)&quot;</span>)</span></code></pre></div>
<p><img src="OneNumOneCatSeveralObs_files/figure-html/unnamed-chunk-7-1.png" width="768" style="display: block; margin: auto;" /></p>
</div>

          <!-- Insert code for related graphics  -->
          <h1>Going further</h1>
          <hr />
          <p>
            You can learn more about each type of graphic presented in this
            story in the dedicated sections. Click the icon below:
          </p>
                    <a href="https://www.data-to-viz.com/#portfolio">
            <img class="mylogoheader"
            src=https://github.com/holtzy/data_to_viz/raw/master/img/section/DensitySmall.png></a
          >
                     <a href="https://www.data-to-viz.com/#portfolio">
            <img class="mylogoheader"
            src=https://github.com/holtzy/data_to_viz/raw/master/img/section/HistogramSmall.png></a
          >
                     <a href="https://www.data-to-viz.com/#portfolio">
            <img class="mylogoheader"
            src=https://github.com/holtzy/data_to_viz/raw/master/img/section/Box1Small.png></a
          >
                     <a href="https://www.data-to-viz.com/#portfolio">
            <img class="mylogoheader"
            src=https://github.com/holtzy/data_to_viz/raw/master/img/section/ViolinSmall.png></a
          >
                     <a href="https://www.data-to-viz.com/#portfolio">
            <img class="mylogoheader"
            src=https://github.com/holtzy/data_to_viz/raw/master/img/section/JoyplotSmall.png></a
          >
                     </div>
    </div>
    <section
      id="form"
      style="
        margin-top: 70px;
        padding-bottom: 70px;
        padding-top: 70px;
        background-color: #f8f9fa;
      "
    >
      <div class="container">
        <div style="display: flex; flex-direction: column; align-items: center">
          <h2 style="font-size: 44px" class="text-center">
            Dataviz decision tree
          </h2>
          <p class="text-center" style="max-width: 600px; font-size: 18px">
            <a href="https://www.data-to-viz.com">Data To Viz</a> is a
            comprehensive <b>classification of chart types</b> organized by data
            input format. Get a high-resolution version of our decision tree
            delivered to your inbox now!
          </p>
          <div
            style="
              margin-top: -50px;
              width: 100%;
              display: flex;
              justify-content: center;
            "
          >
            <script
              async
              data-uid="8010f15fd2"
              src="https://prodigious-trailblazer-3628.ck.page/8010f15fd2/index.js"
            ></script>
          </div>
          <br />
          <div class="text-center">
            <img
              class="img-fluid"
              src="../img/poster/poster_small.png"
              alt="High Resolution Poster"
              style="max-width: 400px"
            />
          </div>
        </div>
      </div>
    </section>

     &nbsp;
<hr />
<p style="text-align: center;">A work by <a href="https://github.com/holtzy/">Yan Holtz</a> for <a href="https://data-to-viz.com">data-to-viz.com</a></p>

<!-- Add icon library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<!-- Add font awesome icons -->
<p style="text-align: center;">
	<a href="https://twitter.com/r_graph_gallery?lang=en" class="fa fa-twitter"></a>
	<a href="https://www.linkedin.com/in/yan-holtz-2477534a/" class="fa fa-linkedin"></a>
	<a href="https://github.com/holtzy/" class="fa fa-github"></a>
</p>

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-79254642-3"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-79254642-3');
</script>
<script>
var getOutboundLink = function(url) {
	gtag('event', 'click', {
		'event_category': 'outbound',
		'event_label': url,
		'transport_type': 'beacon',
		'event_callback': function(){document.location = url;}
	});
}
</script>

&nbsp;   
    <script>

      // add bootstrap table styles to pandoc tables
      function bootstrapStylePandocTables() {
        $('tr.header').parent('thead').parent('table').addClass('table table-condensed');
      }
      $(document).ready(function () {
        bootstrapStylePandocTables();
      });

          </script>
       </body>
</html>
